﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FX.CC.Andon.Entity.ReportManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.ReportManage
{
    /// <summary>
    /// 异常时长汇总图形报表数据层
    /// </summary>
    public class ExceptionSummaryService : DbHelperBase
    {
        public List<ExceptionSummaryEntity> GetExceptionSummaryList(AbnormalQcEntity qcmodel)
        {
            var list = new List<ExceptionSummaryEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT F.*,E.ETypeName FROM ( ");
            strSql.Append(" Select T.ETypeCode, SUM(T.TM-T.ReduceMin*60)/3600.00 TM From(  ");
            strSql.Append(" SELECT B.ETypeCode,B.EqID,B.IsCancle,B.EndTime,B.BizDate,B.TM,B.ReduceMin FROM ( ");
            strSql.Append(" SELECT EqID,IsCancle,ETypeCode,EndTime,BizDate,datediff(second, StartTime, EndTime) as TM,ReduceMin FROM UT_EventRec)AS B    ");
            strSql.Append(" INNER JOIN UT_Equipment AS C ON B.EqID=C.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS D ON C.LineID=D.LineID ");
            strSql.Append(" WHERE B.EndTime IS NOT NULL AND B.IsCancle=0 ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" AND B.BizDate >=@SBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" AND B.BizDate <=@EBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" AND B.EqID='" + qcmodel.EqID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND C.LineID ='" + qcmodel.LineID + "'");
            }
            strSql.Append(" )AS T GROUP BY T.ETypeCode ");
            strSql.Append(" ) AS F ");
            strSql.Append(" LEFT JOIN UT_EType AS E ON F.ETypeCode=E.ETypeCode ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        Db.AddInParameter(cmd, "@SBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateFrom));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        Db.AddInParameter(cmd, "@EBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateEnd));
                    }
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ExceptionSummaryEntity
                                            {
                                                ETypeName =
                                                    reader["ETypeName"] == obj
                                                        ? null
                                                        : Convert.ToString(reader["ETypeName"])
                                            };
                            if (reader["TM"] != DBNull.Value)
                                model.Tm = Math.Round((decimal)reader["TM"], 2);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
